/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.widget;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AbsListView.LayoutParams;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.mol.market.R;
import com.mol.market.demo.widget.TabHostICS.AddTabCallback;

/**
 * 滑动时自动消失的TabHost
 * @author Chaos	
 * @Description TODO
 * @date 2012-12-20 
 */
public class AutoHideTabFragment extends TabFragmentWrapper
                implements AddTabCallback,AnimationListener{
    
    private TabHostICS mTabHost;
    private View       mContent;

    @Override
    protected void initTabs(TabHostICS tabHost) {
       this.mTabHost = tabHost;
       tabHost.setAddTabCallback(this);
    }
    
    @Override
    protected View getCommonTab(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.common_autohide_tabs, null);
    }

    @Override
    public void addTabBefore(TabHostICS tabHost) {
        //do nothing
    }

    @Override
    public void addTabAfter(TabHostICS tabHost) {
        FrameLayout content = (FrameLayout) tabHost.findViewById(R.id.tabrealcontent);
        content.setPadding(0, measureIfNeed(tabHost.getTabWidget()).getMeasuredHeight(), 0, 0);
        
        mContent = content;
    }
    
    public void hide(){
        
        View indicator = mTabHost.getTabWidget();
        
        if(indicator.getVisibility() != View.VISIBLE){
            return ;
        }
        
        if (indicator.getAnimation() == null || indicator.getAnimation().hasEnded()) {
            mContent.setPadding(0, 0, 0, 0);
            indicator.startAnimation(getAnimation(true));
        }
       
    }
    
    public void show(){
        View indicator = mTabHost.getTabWidget();
        
        if(indicator.getVisibility() != View.GONE){
            return ;
        }
        
        if (indicator.getAnimation() == null || indicator.getAnimation().hasEnded()) {
            mContent.setPadding(0, measureIfNeed(mTabHost.getTabWidget()).getMeasuredHeight(), 0, 0);
            mTabHost.getTabWidget().startAnimation(getAnimation(false));
        }
    }
    
    private Animation mOut;
    private Animation mIn;
    
    private Animation getAnimation(boolean out){
        if(out){
            if(mOut == null){
                mOut = new AlphaAnimation(1.0f, 0.0f);
                mOut.setDuration(1000);
                mOut.setFillAfter(true);
                mOut.setAnimationListener(this);
            }
            return mOut;
        }else{
            if(mIn == null){
                mIn = new AlphaAnimation(0.0f, 1.0f);
                mIn.setDuration(1000);
                mIn.setFillAfter(true);
                mIn.setAnimationListener(this);
            }
            return mIn;
        }
    }
    
    private View measureIfNeed(View target){
        
        if(target.getMeasuredHeight() != 0){
            return target;
        }
        
        // i don't know why here will throw an exception if you are using "RelativeLayout"
        // but I can do is just found and in advance this problem
        // by chaos
        // at 2012-11-23
        if (target instanceof RelativeLayout) {
            throw new RuntimeException(
                    "don't use 'RelativeLayout' outside,Please check your 'HeaderView'");
        }

        android.view.ViewGroup.LayoutParams lp = target.getLayoutParams();

        if (lp == null) {
            lp = new LayoutParams(LayoutParams.MATCH_PARENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        int width = lp.width;
        int height = lp.height;
        // create MeasureSpec
        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
        int heightMeasureSpec = 0;
        if (height == 0) {
            // base measureView self...
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED);
        } else {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        }

        target.measure(widthMeasureSpec, heightMeasureSpec);
        return target;
    }

    @Override
    public void onAnimationStart(Animation animation) {
        
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        View indicator = mTabHost.getTabWidget();
        switch (indicator.getVisibility()) {
        case View.VISIBLE:
            indicator.setVisibility(View.GONE);
            break;
        case View.GONE:
            indicator.setVisibility(View.VISIBLE);
            break;
        }

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    protected void onOrientationChange(Orientation oldOri, Orientation newOri) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
